package de.armar.belt_timer;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;

import layout.TableLayout;

public class TimerButton extends JPanel {
    private static final long serialVersionUID = 1L;
    private JButton button;
    private Timer timer;
    private Timer refreshTimer;
    private String name;
    private long starttime;
    private JPanel cardpane;
    private boolean yellow = false;
    private JProgressBar progressBar;
    private CardLayout cards;
    private boolean disabled = false;

    private final static long MINUTE = (long) (60000);
    private final static long COUNTDOWN = MINUTE * 20;
    private static final long YELLOW_TIME = MINUTE * 2;
    private final double[][] tbsize = new double[][] {
	    { TableLayout.FILL, TableLayout.MINIMUM, TableLayout.MINIMUM }, // X
	    { TableLayout.FILL } };
    private JButton cancelbutton;
    private boolean reverse = false;
    private String tmpString = "";
    private JButton belt_empty_button;

    public TimerButton(final String text) {
	this.name = text;

	this.button = new JButton(name.length() == 1 ? ("(0" + name + ")    ")
		: ("(" + name + ")    "));
	button.setForeground(Color.BLACK);
	this.button.setHorizontalAlignment(JButton.LEFT);
	this.setLayout(new TableLayout(tbsize));
	this.cardpane = new JPanel();
	cardpane.setLayout(this.cards = new CardLayout());
	this.add(cardpane, "0,0");
	this.cancelbutton = new JButton(new ImageIcon(
		"./src/de/armar/belt_timer/icons/mail_delete.png"));
	this.cancelbutton.setMinimumSize(new Dimension(30, 1));
	this.cancelbutton.setMaximumSize(new Dimension(30, 100));
	this.cancelbutton.setEnabled(false);
	this.cancelbutton.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent arg0) {
		TimerButton.this.reset();
	    }
	});
	this.add(cancelbutton, "1,0");
	cancelbutton.setFocusPainted(false);
	// --
	this.belt_empty_button = new JButton(new ImageIcon(
		"./src/de/armar/belt_timer/icons/flag_green.png"));
	this.belt_empty_button.setMinimumSize(new Dimension(30, 1));
	this.belt_empty_button.setMaximumSize(new Dimension(30, 100));
	// this.belt_empty_button.setEnabled(false);
	this.belt_empty_button.addActionListener(new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent arg0) {
		if (!disabled) {
		    TimerButton.this.button.setText(name.length() == 1 ? ("(0"
			    + name + ")    ") : ("(" + name + ")    "));
		    TimerButton.this.refreshTimer.stop();
		    TimerButton.this.button.setBackground(new Color(220, 220,
			    220));
		    TimerButton.this.button.setForeground(Color.LIGHT_GRAY);
		    TimerButton.this.belt_empty_button.setIcon(new ImageIcon(
			    "./src/de/armar/belt_timer/icons/flag_yellow.png"));
		    TimerButton.this.disabled = true;
		} else {
		    TimerButton.this.button.setForeground(Color.BLACK);
		    TimerButton.this.button.setBackground(Color.GREEN);
		    TimerButton.this.belt_empty_button.setIcon(new ImageIcon(
			    "./src/de/armar/belt_timer/icons/flag_green.png"));
		    TimerButton.this.disabled = false;
		}
	    }
	});
	this.add(belt_empty_button, "2,0");
	belt_empty_button.setFocusPainted(false);
	// --
	cardpane.add(button, "BUTTON");

	this.progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0,
		(int) COUNTDOWN);
	progressBar.setValue((int) COUNTDOWN);
	this.progressBar.setStringPainted(true);
	this.progressBar.setString("");
	this.progressBar.setFont(new Font("Sans Serif", Font.BOLD, 12));
	cardpane.add(progressBar, "PROGRESS");
	cards.show(cardpane, "BUTTON");
	button.setFocusPainted(false);
	TimerButton.this.button.setBackground(Color.GREEN);
	TimerButton.this.button.setFont(new Font("Verdana", Font.BOLD, 10));
	this.timer = new Timer((int) COUNTDOWN, new ActionListener() {
	    @Override
	    public void actionPerformed(final ActionEvent evt) {
		reset();
		startLastSpawnTimer();
	    }
	});
	this.refreshTimer = new Timer(200, new ActionListener() {
	    @Override
	    public void actionPerformed(final ActionEvent evt) {
		refresh();
	    }
	});
	this.button.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		start();
	    }
	});
    }

    protected void startLastSpawnTimer() {
	TimerButton.this.starttime = System.currentTimeMillis();
	this.reverse = true;
	this.refreshTimer.start();
    }

    protected void refresh() {
	this.progressBar.setValue((int) getCountdownLong());
	this.progressBar.setString(getCountdownString());

	if (getCountdownLong() < YELLOW_TIME && !yellow) {
	    this.progressBar.setBackground(Color.YELLOW);
	    yellow = true;
	}
	if (reverse) {
	    if (!this.tmpString.equals(getCountdownString())) {
		this.tmpString = getCountdownString();
		this.button.setText(name.length() == 1 ? ("(0" + name
			+ ")      " + tmpString)
			: ("(" + name + ")      " + tmpString));
	    }
	}
	this.revalidate();
	this.repaint();
    }

    protected String getCountdownString() {
	Calendar zeit = Calendar.getInstance();
	if (!reverse)
	    zeit.setTimeInMillis(COUNTDOWN
		    - (System.currentTimeMillis() - TimerButton.this.starttime));
	if (reverse)
	    zeit.setTimeInMillis((System.currentTimeMillis() - TimerButton.this.starttime));
	String sekunden = "" + zeit.get(Calendar.SECOND);
	String minuten = "" + zeit.get(Calendar.MINUTE);
	if (sekunden.length() == 1)
	    sekunden = "0" + sekunden;
	if (minuten.length() == 1)
	    minuten = "0" + minuten;
	return (reverse ? (minuten + ":" + sekunden.charAt(0) + "0") : (minuten
		+ ":" + sekunden));
    }

    protected long getCountdownLong() {
	return COUNTDOWN
		- (System.currentTimeMillis() - TimerButton.this.starttime);
    }

    protected void start() {
	reverse = false;
	TimerButton.this.button.setBackground(Color.LIGHT_GRAY);
	TimerButton.this.timer.start();
	TimerButton.this.starttime = System.currentTimeMillis();
	TimerButton.this.refreshTimer.start();
	cards.show(cardpane, "PROGRESS");
	this.cancelbutton.setEnabled(true);
	this.belt_empty_button.setEnabled(false);
	this.progressBar.setBackground(new Color(245, 245, 245));
	yellow = false;
	disabled = false;
    }

    public void reset() {
	this.belt_empty_button.setEnabled(true);
	TimerButton.this.timer.stop();
	yellow = false;
	this.progressBar.setString("");
	TimerButton.this.refreshTimer.stop();
	TimerButton.this.button.setBackground(Color.GREEN);
	this.cards.show(cardpane, "BUTTON");
	this.progressBar.setValue((int) COUNTDOWN);
	this.cancelbutton.setEnabled(false);
	this.button.setText(name.length() == 1 ? ("(0" + name + ")    ") : ("("
		+ name + ")    "));
	if (disabled) {
	    TimerButton.this.button.setBackground(Color.GREEN);
	    TimerButton.this.button.setForeground(Color.BLACK);
	    TimerButton.this.belt_empty_button.setIcon(new ImageIcon(
		    "./src/de/armar/belt_timer/icons/flag_green.png"));
	    TimerButton.this.disabled = false;

	}
    }

}
